import { reactive, ref, watch } from 'vue';
// import { uni } from '@dcloudio/uni-app';
import { request } from '@/utils/request'
import { tokenManager } from '@/utils/tokenManager';

export function useDatabase() {
    const workflow_database_id = '7472973211970945074';
    const workflow_img_id = '7472973252546150415';
    const workflow_delete_id = '7473434367755616271';

    const pageList = ref([]);
    const pageData = reactive({
        page: 1,
        size: 100,
        total: 0
    });

    const resources = ref([]);

    async function createImage(param) {
        try {
            const body = {
                parameters: {
                    fat: false,
                    heel: false,
                    old: 40,
                    colors: "黑色 蓝色",
                    ...param
                },
                workflow_id: workflow_img_id,
                is_async: false,
            };

            const response = await uni.request({
                url: tokenManager.getCozeApiUrl(),
                method: 'POST',
                header: tokenManager.getHeaders(),
                data: body,
            });

            if (response.statusCode !== 200) {
                throw new Error(`${response.data.msg}`);
            }

            const obj = response.data;

            console.log('obj', obj);
            if (obj?.data) {
                const aim = JSON.parse(obj.data)
                console.log('aim', aim);
                return aim.output
            }
        } catch (error) {
            console.error('Error:', error);
            uni.showToast({ title: error, icon: 'none' });
        }
        return []
    }

    async function reset(init = true) {
        if (init) {
            pageData.page = 1;
        }
        loadMore(pageData.page)
    }

    async function loadMore(n) {
        pageList.value = [];
        pageData.page = n;
        const list = await loadPageData();
        pageList.value = list;
    }

    async function deleteImg(id) {
        try {
            const body = {
                parameters: {
                    input: id,
                },
                workflow_id: workflow_delete_id,
                is_async: false,
            };

            const response = await uni.request({
                url: tokenManager.getCozeApiUrl(),
                method: 'POST',
                header: tokenManager.getHeaders(),
                data: body,
            });
            // if (response.statusCode!== 200) {
            //     throw new Error(`HTTP error! status: ${response.statusCode}`);
            // }
            const obj = response.data;

            console.log('obj', obj);
            if (obj?.data) {
                const aim = JSON.parse(obj.data)
                console.log('aim', aim);
                return aim;
            }

        } catch (error) {
            return null
        }
        return null
    }

    async function loadPageData() {
        try {
            const body = {
                parameters: {
                    page: pageData.page,
                    size: pageData.size
                },
                workflow_id: workflow_database_id,
                is_async: false,
            };

            const response = await uni.request({
                url: tokenManager.getCozeApiUrl(),
                method: 'POST',
                header: tokenManager.getHeaders(),
                data: body,
            });

            if (response.statusCode !== 200) {
                throw new Error(`${response.data.msg}`);
            }

            const obj = response.data;

            console.log('obj', obj);
            const aim = JSON.parse(obj.data)
            console.log('aim', aim);
            pageData.total = Number(aim.totalList[0].total)
            return aim.output
        } catch (error) {
            console.error('Error:', error);
            uni.showToast({ title: error.message, icon: 'none' });
        }
    }



    return {
        pageList,
        pageData,
        reset,
        createImage,
        loadMore,
        deleteImg
    };
}